﻿using Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using ServiceCore.Tool.Post;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ServiceCore.Tool.Wx.customservice
{

    public interface Icustom_service
    {
        /// <summary>
        /// 获取客服列表
        /// </summary>
        /// <returns></returns>
        getkflist_result getkflist();
        /// <summary>
        /// 添加客服
        /// </summary>
        /// <returns></returns>
        kf_add_result add(string kf_account, string nickname);

        /// <summary>
        /// 邀请客服
        /// </summary>
        /// <param name="kf_account"></param>
        /// <param name="invite_wx"></param>
        /// <returns></returns>
        inviteworker_result inviteworker(string kf_account, string invite_wx);
        /// <summary>
        /// 更新客服信息
        /// </summary>
        /// <param name="kf_account"></param>
        /// <param name="nickname"></param>
        /// <returns></returns>
        kf_update_result kf_update(string kf_account, string nickname);
        /// <summary>
        /// 上传客服头像
        /// </summary>
        /// <param name="kf_account"></param>
        /// <param name="media"></param>
        /// <returns></returns>
        uploadheadimg_result uploadheadimg(string kf_account, IFormFile media);

        /// <summary>
        /// 创建会话
        /// </summary>
        /// <param name="kf_account"></param>
        /// <param name="openid"></param>
        /// <returns></returns>
        create_session_result create_session(string kf_account, string openid);
        /// <summary>
        /// 关闭会话
        /// </summary>
        /// <param name="kf_account"></param>
        /// <param name="openid"></param>
        /// <returns></returns>
        close_session_result close_session(string kf_account, string openid);
        /// <summary>
        /// 获取指定客服已接入会话列表
        /// </summary>
        /// <param name="kf_account"></param>
        /// <returns></returns>
        getsessionlist_result getsessionlist(string kf_account);
        /// <summary>
        /// 获取未接入会话列表
        /// </summary>
        /// <returns></returns>
        getwaitcase_result getwaitcase();
        /// <summary>
        /// 获取会话
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        getsession_result getsession(string openid);

        /// <summary>
        /// 文本消息发送
        /// </summary>
        /// <param name="openid"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        send_text_result send_text(string kf_account, string openid, string content);


    }
    public class custom_service : Icustom_service
    {
        private readonly IConfiguration AppSettingConfig; 
        public custom_service(IConfiguration configuration)
        {
            AppSettingConfig = configuration;
        }
        public string get_access_token_api_url { get { return AppSettingConfig["get_access_token_api"]; } }
        /// <summary>
        /// 获取accesstoken
        /// </summary>
        private string get_accesstoken
        {
            get
            {
                string access_token = Http_Helper.HttpGet(get_access_token_api_url + $"?who=rk_{DateTime.Now.ToString("yyyy-MM:dd")}");
                if (access_token == "小朋友，你是不是有许多问号")
                {
                    access_token = "";
                }
                return access_token;
            }
        } 
        #region 请求方法
        /// <summary>
        ///  获取客服列表
        /// </summary>
        public getkflist_result getkflist()
        {
            getkflist_result wx_result = new api_base().create_option(custom_config.Api_Url.getkflist, get_accesstoken).http_client<getkflist_params, getkflist_result>(null);
            return wx_result;
        }
        /// <summary>
        ///  获取客服列表
        /// </summary>
        public kf_add_result add(string kf_account, string nickname)
        {
            kf_add_result wx_result = new api_base().create_option(custom_config.Api_Url.add, get_accesstoken).http_client<kf_add_params, kf_add_result>(new kf_add_params()
            {
                kf_account = kf_account,
                nickname = nickname
            }, false);
            return wx_result;
        }
        /// <summary>
        /// 邀请绑定客服帐号
        /// </summary>
        public inviteworker_result inviteworker(string kf_account, string invite_wx)
        {
            inviteworker_result wx_result = new api_base().create_option(custom_config.Api_Url.inviteworker, get_accesstoken).http_client<inviteworker_params, inviteworker_result>(new inviteworker_params()
            {
                kf_account = kf_account,
                invite_wx = invite_wx
            }, false);
            return wx_result;
        }
        /// <summary>
        /// 设置客服信息
        /// </summary>
        public kf_update_result kf_update(string kf_account, string nickname)
        {
            kf_update_result wx_result = new api_base().create_option(custom_config.Api_Url.update, get_accesstoken).http_client<kf_update_params, kf_update_result>(new kf_update_params()
            {
                kf_account = kf_account,
                nickname = nickname
            }, false);
            return wx_result;
        }
        /// <summary>
        /// 设置客服信息
        /// </summary>
        public uploadheadimg_result uploadheadimg(string kf_account, IFormFile media)
        {
            var ext_params =  new Dictionary<string, string>();
            ext_params.Add("kf_account", kf_account);
            uploadheadimg_result wx_result = new api_base().create_option(custom_config.Api_Url.uploadheadimg, get_accesstoken).post_upload<uploadheadimg_params, uploadheadimg_result>(media, ext_params);
            return wx_result;
        } 
        /// <summary>
        /// 删除客服账号
        /// </summary>
        public kf_delete_result kf_delete(string kf_account)
        {
            kf_delete_result wx_result = new api_base().create_option(custom_config.Api_Url.del, get_accesstoken).http_client<kf_delete_params, kf_delete_result>(new kf_delete_params()
            {
                kf_account = kf_account, 
            });
            return wx_result;
        } 
        /// <summary>
        /// 开启会话
        /// </summary>
        public create_session_result create_session(string kf_account, string openid)
        {
            create_session_result wx_result = new api_base().create_option(custom_config.Api_Url.create_session, get_accesstoken).http_client<create_session_params, create_session_result>(new create_session_params()
            {
                kf_account = kf_account,
                openid = openid
            }, false);
            return wx_result;
        } 
        /// <summary>
        /// 关闭会话
        /// </summary>
        public close_session_result close_session(string kf_account, string openid)
        {
            close_session_result wx_result = new api_base().create_option(custom_config.Api_Url.close_session, get_accesstoken).http_client<close_session_params, close_session_result>(new close_session_params()
            {
                kf_account = kf_account,
                openid = openid
            }, false);
            return wx_result;
        } 
        /// <summary>
        /// 获取客户会话状态
        /// </summary>
        public getsession_result getsession(string openid)
        {
            getsession_result wx_result = new api_base().create_option(custom_config.Api_Url.getsession, get_accesstoken).http_client<getsession_params, getsession_result>(new getsession_params()
            {
                openid = openid
            });
            return wx_result;
        } 
        /// <summary>
        /// 获取客服会话列表
        /// </summary>
        public getsessionlist_result getsessionlist(string kf_account)
        {
            getsessionlist_result wx_result = new api_base().create_option(custom_config.Api_Url.getsessionlist, get_accesstoken).http_client<getsessionlist_params, getsessionlist_result>(new getsessionlist_params()
            {
                kf_account = kf_account
            }, true);
            return wx_result;
        }
        /// <summary>
        /// 获取客服未接入会话列表
        /// </summary>
        public getwaitcase_result getwaitcase()
        {
            getwaitcase_result wx_result = new api_base().create_option(custom_config.Api_Url.getwaitcase, get_accesstoken).http_client<getwaitcase_params, getwaitcase_result>(null, false);
            return wx_result;
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        public send_text_result send_text(string kf_account, string openid, string content)
        {
            send_text_result wx_result = new api_base().create_option(custom_config.Api_Url.send, get_accesstoken).http_client<send_text_params, send_text_result>(new send_text_params()
            {
                touser = openid,
                msgtype = "text",
                text = new { content },
                customservice = new { kf_account }
            }, false);
            return wx_result;
        }
        #endregion

    }
}
